package com.qo.questiononline.controller;

import com.alibaba.fastjson.JSONObject;
import com.qo.questiononline.mapper.QlLibMapper;
import com.qo.questiononline.mapper.QlQuestionsMapper;
import com.qo.questiononline.model.QlLib;
import com.qo.questiononline.model.QlQuestions;
import com.qo.questiononline.utils.ResMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * api
 *
 * @author eq
 * @version 1.0.0
 */
@Slf4j
@Controller
@RequestMapping("/api")
public class QuestionController {

    /**
     * 题库操作
     */
    private QlLibMapper qlLibMapper;
    /**
     * 问题操作
     */
    private QlQuestionsMapper qlQuestionsMapper;

    @Autowired
    public void setQlLibMapper(QlLibMapper qlLibMapper) {
        this.qlLibMapper = qlLibMapper;
    }

    @Autowired
    public void setQlQuestionsMapper(QlQuestionsMapper qlQuestionsMapper) {
        this.qlQuestionsMapper = qlQuestionsMapper;
    }

    /**
     * 查询测试
     *
     * @return {String} 无意义的字符串
     */
    @RequestMapping("question")
    @ResponseBody
    public String getQuestion() {

        return "question";
    }

    /**
     * 查询测试
     *
     * @return {String} 无意义的字符串
     */
    @RequestMapping("pub/questionLib")
    @ResponseBody
    public String getQuestionLib() {

        return "questionLib";
    }

    /**
     * 获取问题库
     *
     * @param qlLib 问题库查询条件
     * @return {Json} json字符串
     */
    @RequestMapping("pub/getLib")
    @ResponseBody
    public String getLib(@RequestBody QlLib qlLib) {
        ResMessage resMessage = new ResMessage();
        List<QlLib> qlLibList = qlLibMapper.getLib(qlLib);

        return resMessage.sysSuccess(qlLibList);
    }

    /**
     * 向题库添加问题
     *
     * @param qlQuestions 问题信息
     * @return {Json} json字符串
     */
    @RequestMapping("addQuestion")
    @ResponseBody
    public String addQuestion(HttpServletRequest request, @RequestBody QlQuestions qlQuestions) {
        HttpSession session = request.getSession(); // 获取浏览器Session
        String userInfo = (String) session.getAttribute("userInfo"); // 取的userInfo中的用户信息
        String users[] = userInfo.split(",");
        qlQuestions.setCreatedBy(users[1]); // 创建者
        Date d = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        qlQuestions.setCreatedTime(simpleDateFormat.format(d)); // 创建时间
        ResMessage resMessage = new ResMessage();
        qlQuestionsMapper.addQuestion(qlQuestions);

        return resMessage.sysSuccess();
    }

    /**
     * 查询问题列表
     *
     * @param qlQuestions 查询条件
     * @return {Json} json字符串
     */
    @RequestMapping("pub/getQuestions")
    @ResponseBody
    public String getQuestions(@RequestBody QlQuestions qlQuestions) {
        ResMessage resMessage = new ResMessage();
        List<QlQuestions> qlQuestionsList = qlQuestionsMapper.getQuestions(qlQuestions);

        return resMessage.sysSuccess(qlQuestionsList);
    }

    /**
     * 登陆后查询问题列表
     *
     * @param request     请求
     * @param qlQuestions 查询条件
     * @return {Json} json字符串
     */
    @RequestMapping("getQuestions")
    @ResponseBody
    public String apiGetQuestions(HttpServletRequest request, @RequestBody QlQuestions qlQuestions) {
        HttpSession session = request.getSession(); // 获取浏览器Session
        String userInfo = (String) session.getAttribute("userInfo"); // 取的userInfo中的用户信息
        String users[] = userInfo.split(",");
        qlQuestions.setCreatedBy(users[1]); // 创建者
        ResMessage resMessage = new ResMessage();
        List<QlQuestions> qlQuestionsList = qlQuestionsMapper.getQuestions(qlQuestions);

        return resMessage.sysSuccess(qlQuestionsList);
    }
}
